package com.hs.jhzd.controller;

import com.hs.jhzd.entity.HqRycdxxbEntity;
import com.hs.jhzd.entity.ZfCpgdxxbEntity;
import com.hs.jhzd.entity.ZfSgdpbxxbEntity;
import com.hs.jhzd.repository.HqRycdxxbRepository;
import com.hs.jhzd.repository.ZfCpgdxxbRepository;
import com.hs.jhzd.repository.ZfSgdpbxxbRepository;
import com.hs.jhzd.utils.DateUtils;
import com.hs.jhzd.utils.enums.DayOfWeekFullEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Controller
@RequestMapping("/acp-cloud/select")
@Slf4j
public class SchedulingController {

    @Autowired
    private ZfCpgdxxbRepository zfCpgdxxbRepository;

    @Autowired
    private HqRycdxxbRepository hqRycdxxbRepository;

    @Autowired
    private ZfSgdpbxxbRepository zfSgdpbxxbRepository;

    //床位
    @RequestMapping("/sgd/list")
    @ResponseBody
    public List<ZfCpgdxxbEntity> bed(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
        return zfCpgdxxbRepository.findByJsbhAndJshAndScbj(jsbh, jsh, false);
    }
    //值日列表
    @RequestMapping("/duty/list")
    @ResponseBody
    public List<ZfSgdpbxxbEntity> duty(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
        Date mondayOfWeek = DateUtils.getMondayOfWeek();
        Date sundayOfWeek = DateUtils.getSundayOfWeek();
        return zfSgdpbxxbRepository.findByJsbhAndJshByRq(jsbh, jsh, mondayOfWeek, sundayOfWeek);
    }

    //值班列表
    @RequestMapping("/shift/list")
    @ResponseBody
    public List<ZfSgdpbxxbEntity> shift(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
        Date mondayOfWeek = DateUtils.getMondayOfWeek();
        Date sundayOfWeek = DateUtils.getSundayOfWeek();
        return zfSgdpbxxbRepository.findShiftByRq(jsbh, jsh, mondayOfWeek, sundayOfWeek);
    }
    //每周菜谱
    @RequestMapping("/food/list")
    @ResponseBody
    public List<HqRycdxxbEntity> food(@RequestParam(required = true) String jsbh) {
        Date mondayOfWeek = DateUtils.getMondayOfWeek();
        Date sundayOfWeek = DateUtils.getSundayOfWeek();
        return hqRycdxxbRepository.findByRq(mondayOfWeek, sundayOfWeek);
    }

    @RequestMapping("/shift/todayShift")
    @ResponseBody
    public List<ZfSgdpbxxbEntity> todayShift(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
//        Date mondayOfWeek = DateUtils.getMondayOfWeek();
//        Date sundayOfWeek = DateUtils.getSundayOfWeek();
        String week = getDayOfWeek();
        return zfSgdpbxxbRepository.findByJsbhAndJshAndXqOrderByRqDesc(jsbh, jsh, week);
    }


    private String getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        // 如果当前时间小于上午9点，取前一天的值班
        int nowHour = calendar.get(Calendar.HOUR_OF_DAY);
        if (nowHour <= 9) {
            // 9点前，且是周一查询上周日的值班信息
            if (dayOfWeek == 2) {
                dayOfWeek = 1;
            } else {
                dayOfWeek -= 1;
            }
        }
        return DayOfWeekFullEnum.getValue(dayOfWeek);
    }

}
